package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.LogBean;
import com.project.mapper.ILogMapper;
import com.project.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

@Service
@Transactional
public class LogServiceImp implements ILogService {
    @Autowired
    private ILogMapper mapper;
    @Override
    public Page<LogBean> cutByItem(Integer pageNO, LocalDate startDate, LocalDate endDate) {
        QueryWrapper qw =new QueryWrapper<>();
        if(startDate!=null) {
            qw.ge("l_date", startDate);
        }
        if(endDate!=null) {
            qw.le("l_date", endDate);
        }
        return mapper.selectPage(new Page<>(pageNO,5),qw);
    }


    @Override
    public void add(String loginUser) {
        LogBean logBean=new LogBean();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String msg = sdf.format(new Date());
        String logStr = "用户:"+loginUser+"在"+msg+"登录系统";
        logBean.setContent(logStr);
        mapper.insert(logBean);
    }
}
